package com.loveprogrammer.battle.action;

import com.loveprogrammer.battle.Hero;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName BaseAttack
 * @Description TODO
 * @Author admin
 * @Date 2024/2/20 17:43
 * @Version 1.0
 */
public class GroupAttack extends Attack {

    private Hero heroA;

    private List<Hero> defenceList;

    public GroupAttack(Hero heroA, List<Hero> defenceList) {
        this.heroA = heroA;
        this.defenceList = defenceList;
        setIntervalTime(computeInterval(heroA.getSpeed()));
        setSpeed(heroA.getSpeed());
    }

    @Override
    public boolean run() {
        // 自己血量少于0 返回
        if(heroA.getHp() > 0) {        // 遍历并找到血量最少的攻击
            defenceList = defenceList.stream().filter(e -> e.getHp() > 0).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(defenceList)) {
                defenceList.sort(Comparator.comparing(Hero::getHp));
                heroA.attack(defenceList.get(0));
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean checkContinue() {
        return heroA.getHp() > 0 && defenceList.stream().anyMatch(e -> e.getHp() > 0);
    }

}
